package com.example.yyjc.config;

import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName EncryptorConfig
 * @Description TODO  配置《配置文件加解密》
 * @Author lvyongwei
 * @Date 2020-12-18 15：04
 **/
@Configuration
public class EncryptorConfig {

    @Bean("jasyptStringEncryptor")
    public StringEncryptor jasyptStringEncryptor() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        //config.setPassword("wangmaox");
        config.setPassword("lvyongwei");
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        return encryptor;
    }

    @Autowired
    @Qualifier("jasyptStringEncryptor")
    /**
     * @Resource(name = "jasyptStringEncryptor")
     */
    private StringEncryptor stringEncryptor;

    public String getPassword(){
        System.out.println(stringEncryptor);
        return stringEncryptor.encrypt("root");
        //return stringEncryptor.encrypt("jdbc:mysql://localhost:3306/yyjc?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&useSSL=false");
    }
}
